#!/bin/bash
#date=2016/4/11
#author=junquan.su@jwis.cn
#version v1.0
#本说明：每天凌晨4点备份数据库到/data/backup/databak目录，只保留三天的文件纪录。

#将备份目录添加进数组中
DATA=("mate_app mate_user mate_news mate-search insight innovation market jwi_discuz Cobub_Razor Cobub_RazorHub pydata houses robot mongo")

#设置备份路径与备份数据库用户相关
backup_dir=/data/backup/databak
MY_USER=root
MY_PASSWD=JWadmin@DB1306

#判断备份目录是否存在，不存在就创建
if [ -d $backup_dir ];then
  cd $backup_dir
else
  mkdir -p $backup_dir
  cd $backup_dir
fi
#遍历备份目录
for data in ${DATA[*]}
do
  if [ $data == "mongo" ];then
        tar zcfP mongo-`date +%F`.tar.gz /var/lib/mongo
        sleep 5
  else
        /usr/local/mysql/bin/mysqldump -u$MY_USER -p$MY_PASSWD $data > $data-`date +%F`.sql 2> /dev/null
        sleep 10
  fi  
done
#删除三天前的备份文件
find $backup_dir -mtime +3 -type f -exec rm -rf {} \;
